package com.examination.controller.stationModel;

import com.examination.dao.stationModel.StationDao;
import com.examination.entity.Station;
import com.examination.service.stationModel.StationService;
import com.examination.util.JsonResult;
import com.examination.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Api(description = "站点接口")
@RestController
@RequestMapping("station")
@CrossOrigin
public class StationController {
    @Autowired
    private StationService stationService;

    @ApiOperation(value = "分页模糊查询站点信息" , notes = "{\"pageNo\": 1, \n" +
            " \"pageSize\":10}")
    @PostMapping("findStation")
    public Page<Station> findStation(@RequestBody Map map) {
        Page<Station> page = new Page<Station>();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(stationService.Total(map));
        page.setItems(stationService.findStation(map));
        return page;
    }

    @ApiOperation(value = "增加站点信息" , notes = "{\"station_name\":\"鹿城路站（06标）\", \"start_time\":\"2019-06-15\", \"end_time\":\"2020-06-15\", \"station_principal\":\"晋铁科技\", \"station_area\":200000}")
    @PostMapping("insertStation")
    public JsonResult add_station(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        map.put("station_time" , stationService.count(map));
        int i = stationService.insertStation(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("增加成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("增加失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "修改站点信息" , notes = "{\"station_name\":\"鹿城路站\",\n" +
            "\"station_time\":365,\n" +
            "\"start_time\":\"2019-06-15\",\n" +
            "\"end_time\":\"2020-06-15\",\n" +
            "\"station_principal\":\"晋铁科技\",\n" +
            "\"station_area\":200000,\n" +
            "\"id\":1}")
    @PostMapping("updateStation")
    public JsonResult updateStation(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        map.put("station_time" , stationService.count(map));
        int i = stationService.updateStation(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("修改成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "删除站点信息" , notes = "测试数据:{\"id\":1}")
    @PostMapping("deleteStation")
    public JsonResult deleteStation(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = stationService.deleteStation(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("删除成功!");
            return jsonResult;
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除失败!");
            return jsonResult;
        }
    }

    @ApiOperation(value = "下拉框查找站点信息" , notes = "测试数据:")
    @PostMapping("selectStation")
    public List<Station> selectStation(@RequestBody Map map) {
        return stationService.selectStation(map);
    }
}
